/*
* @desc 工具函数
* */

export function dateFormatter(formatter, date){
	date = (date ? new Date(date) : new Date)
	const Y = date.getFullYear() + '',
		M = date.getMonth() + 1,
		D = date.getDate(),
		H = date.getHours(),
		m = date.getMinutes(),
		s = date.getSeconds()
	return formatter.replace(/YYYY|yyyy/g, Y)
		.replace(/YY|yy/g, Y.substr(2, 2))
		.replace(/MM/g, (M < 10 ? '0' : '') + M)
		.replace(/M/g, M)
		.replace(/DD/g, (D < 10 ? '0' : '') + D)
		.replace(/HH|hh/g, (H < 10 ? '0' : '') + H)
		.replace(/mm/g, (m < 10 ? '0' : '') + m)
		.replace(/ss/g, (s < 10 ? '0' : '') + s)
}

// 过滤无效属性
export function objScreen(obj) {
	for (let key in obj) {
		if (obj[key] === undefined || obj[key] === '' || obj[key] === null) {
			delete obj[key]
		}
	}
	return obj
}
export function throttle(fn,wait=1000){
	let flag=true;
	return function(){
		let context = this;
		let arg = arguments;
		if(flag){
			flag=false;
			fn.call(context,arg);
			setTimeout(()=>{
				flag=true
			}, wait)
		}
	}
}

/**
 * 深度复制合并
 * @param {*} data 
 * @param {*} newData 
 * @returns object
 */
export function deepAssign(data, newData) {
    for(let [key, value] of Object.entries(newData)) {
        if(Object.prototype.toString.call(value) !== '[object Object]' && Object.prototype.toString.call(value) !== '[object Array]') {
            data[key] = value;
        } else {
            let _data = data[key] || {}
            if(Object.prototype.toString.call(value) === '[object Array]') {
                _data = data[key] || []
            }
            data[key] = deepAssign(_data, value)
        }
    }
    return data;
}

/**
 * 数字单位格式化
 * @param num 
 * @param multiple 
 * @returns {string}
 */
 export function formatterNum(num, multiple = 10000) {
	if(num >= 100000000) {
	    return Math.round(num / 1000000) / 100 + '亿'
    } else if(num >= multiple) {
	    return Math.round(num / 100) / 100 + '万'
    } else {
	    return num
    }
}

/**
 * 数字单位格式化
 * @param num      数据
 * @param multiple 大于指定长度值开始计算
 * @param digit    小数点位数
 * @param rounding 是否四舍五入
 * @param tag
 * @returns {string}
 */
export function formatterNumber(num, multiple = 10000, digit = 2, rounding = true, tag = false) {
	let _d = Math.pow(10, digit)
	let _f = num => parseInt(num)
	let minus = 1
	if(num < 0) {
		minus = -1
		num = Math.abs(num)
	}
	if(rounding) _f = num => Math.round(num)
	// if(rounding) _f = num => Math.ceil(num)
	if(num >= multiple * 10000) {
		const unit = tag ? '<font>亿</font>' : '亿'
	    return minus * _f(num * _d / 100000000) / _d + unit
    } else if(num >= multiple) {
		const unit = tag ? '<font>万</font>' : '万'
	    return minus * _f(num * _d / 10000) / _d + unit
    } else {
	    return minus * _f(num * _d) / _d
    }
}

export function formatterNumFix(num) {
	if(num >= 1e3 && num < 1e6) {
		return Math.ceil(num * 10 / 1e3) / 10 + 'k'
	} else if(num >= 1e6) {
	// } else if(num >= 1e7 && num < 1e8) {
		return Math.ceil(num * 10 / 1e4) / 10 + 'm'
	// } else if( num >= 1e8) {
	// 	return (num / 1e7) + 'kw'
	} else {
		return num
	}
}

export function getWeek(val) {
	const weeks = [
		'周日',
		'周一',
		'周二',
		'周三',
		'周四',
		'周五',
		'周六'
	]
	return weeks[val]
}

export const getChartMin = (data, obj) => {
	if (!data) return 0
	let min = data[0][1]
    const max = data.slice(-1)[0][1]
    let step = parseInt((max - min) / data.length)
    if (step === 0) step = 100
    min = min - data.length * step * 0.8
	min = min < 0 ? 0 : min
	if (obj.yAxis) obj.yAxis['min'] = min
	else obj.yAxis = { min }
}

export const getChartMin2 = (data, obj) => {
	if (!data || data.length <= 0) return
	let min = data[0][1]
    const max = data.slice(-1)[0][1]
    let step = parseInt((max - min) / data.length)
    if (step < 1000) step = 1000
    min = Math.floor(min / 1000) * 1000 - data.length * step / 2
	min = min < 0 ? 0 : min
	if (obj.yAxis) obj.yAxis['min'] = min
	else obj.yAxis = { min }
}

/**
 * 获取随机数
 * @param {number} len - 随机数长度
 * @returns {string}
 */
export const randomString = len => {
	len = len || 32
	const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
	const maxPos = chars.length
	let str = ''
	for (let i = 0; i < len; i++) {
		str += chars.charAt(Math.floor(Math.random() * maxPos))
	}
	return str
}

/**
 * 数组转键值对象
 * @param {array} arr - 原数组
 * @param {string} key - 键值
 * @returns {object}
 */
export const arrayToJson = (arr, key) => {
    const obj = {}
    arr.forEach(item => {
        obj[item[key]] = item
    })
    return obj
}

/**
 * @description 比较
 * @param property {string}
 * @param desc {boolean}
 */
export function compare(property, desc = false) {
    return function (a, b) {
        const val1 = a[property]
        const val2 = b[property]
        if (desc) {
            return val1 - val2 // 升序排列
        } else {
            return val2 - val1 // 降序排列
        }
    }
}

/**
 * @description 过滤非数字
 * @param value {string}
 * @param decimal {number} - 支持小数点几位
 * @return {string}
 */
export function filterNumber(value, decimal = 0) {
    // return decimal == 0 ? str.replace(/[^\d]/g, '') : str.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/, '$1')
    // const re = /^\D*([0-9]\d*\.?\d{0,2})?.*$/
    // const re = new RegExp("^\\D*([0-9]\\d*\.?\\d{0," + decimal + "})?.*$", "") // eslint-disable-line
    // return decimal == 0 ? str.replace(/[^\d]/g, '') : str.replace(re, '$1')
    const re = new RegExp("^[0]\\d|^\\D*([0-9]\\d*\\.?\\d{0," + decimal + "})?.*$", "") // eslint-disable-line
    return decimal == 0 ? value.replace(/^[0]|[^\d]/g, '') : value.replace(re, '$1')  
}